<template>
	<div>
		<AplidForm ref="formRef" v-bind="BaseFormConfig" v-model="formModel" @submit="onSubmit" @reset="back">
			<!-- 下发对象 -->
			<template v-slot:organizationIdListForm>
				<div>
					<div class="flex align-center">
						<el-button type="primary" size="small" @click="handleOpenSelect(1)">刷选器</el-button>
						已选择({{ OrgNum }})
					</div>
				</div>
			</template>
			<!-- 评估对象-机构 -->
			<template v-slot:userIdListForm>
				<div>
					<div class="flex align-center">
						<el-button type="primary" size="small" @click="handleOpenSelect(3)">刷选器</el-button>
						已选择({{ personNum }})
					</div>
				</div>
			</template>
			<template #typeForm>
				<!-- <el-select v-model="formModel.type" @change="selectChange" clearable>
					<el-option v-for="(item,index) in optionList" :key="index" :label="item.label" :value="item.value"></el-option>
				</el-select> -->
				<remoteSelect
				style="width: 100%;"
				clearable
				v-model:model-name="formModel.typeLabel"
				v-model:model-value="formModel.type"
				api="getDictItemList"
				code="OST_QUESTION_BANK_TYPE"
				propName="typeLabel"
				filterKey='value'
				filterLabel="label"
				fileLabel='label'
				@change="selectChange"
				></remoteSelect>
			</template>
			<!-- 有图库分类得到题库 -->
			<template  #bankIdForm>
				<remoteSelect
				:key="typeChange"
				v-model:model-name="formModel.bankCode"
				v-model:model-value="formModel.bankId"
				api='view_bscQuestionBank_list'
				type='remoteSelect'
				:paramsObj="{type:typeVal,isOpen:1}"
				propName="code"
				filterKey='id'
				filterLabel="code"
				fileLabel='code'
				@change="bankChange"
				></remoteSelect>
			</template>
		</AplidForm>

		<el-drawer v-model="drawer" :close-on-click-modal="false" title="选择考试对象" size="60%" :lock-scroll="false" :modal="false" append-to-body>
			<practitioners
				ref="evaluator"
				v-show="listType == 3"
				:key="key"
				:listType="listType"
				@selection-change="onSelectionChange"
				:selectedData="selectedData"
			>
			</practitioners>

			<serviceOrgan
				ref="serviceOrg"
				v-show="listType == 1"
				:key="key"
				:listType="listType"
				@selection-change="onSelectionChange"
				
				:selectedData="selectedData"
			>
			</serviceOrgan>
			<template #footer>
				<div class="f-between w-100">
					<p>请勾选</p>
					<el-button type="primary" @click="drawer = !drawer">返回</el-button>
				</div>
			</template>
		</el-drawer>
	</div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
// import Filters from '../Filters/index.vue'
import apiMod from '@/apiMod/index'
import { BaseFormConfig } from '../config'
import practitioners from '../Filters/practitioners.vue'
import serviceOrgan from '../Filters/serviceOrgan.vue'
import remoteSelect from '@/components/AplidCrud/src/AplidRemote/index'

type Props = {
	config?: any
	nowRowData?: any
	stepNum?: any
	formData?: any
	formType?: any
}
const props = withDefaults(defineProps<Props>(), {
	config: {},
	nowRowData: {},
	stepNum: 1,
	formData: {},
	formType: 'add',
})

const bankChange =(data) =>{
	if(data){
		apiMod.view_bscQuestionBank_view(data.id).then(res=>{
			formModel.value.itemNum = res.data.itemNum || 0
			formModel.value.itemScore = res.data.itemScore || 0
		})
	}
} 
// 改变
let typeChange = ref(1)
let typeVal = ref(undefined)

const selectChange = (e) =>{
	typeVal.value = e.value
	formModel.value.belongApp = e.value // 划分图库类型
	typeChange.value = Math.random()
}
const back = ()=>{
	emit('refresh')
}

const emit = defineEmits(['refresh', 'gatherParams', 'back'])
// 需要两个对象来存储数据
let OrgNum = ref(0)
let personNum = ref(0)
// 获取到抽屉数据
let key = ref(1)
let formModel: any = ref({})
let drawer = ref(false)
// 通过接口获取勾选的数据
const getApiSelect = async (editData, api) => {
	console.log('进入')
	if (editData) {
		let list = editData.split(',')
		let params = {
			idList: list,
			pageParaPO: { size: 100000, current: 1 },
		}
		let res = await apiMod[api](params)
		console.log('勾选数据', res)
		selectedData.value = res.data.records

		key.value = Math.random()
		// drawer.value  = true
		return res.data.records
	} else {
		return []
		// drawer.value  = true
	}
}
// 初始化列表和数据
if (props.formType == 'edit') {
	if (props.nowRowData.organizationIdList) {
		let list = props.nowRowData.organizationIdList.split(',')
		OrgNum.value = list.length
	}
	if (props.nowRowData.userIdList) {
		let list = props.nowRowData.userIdList.split(',')
		personNum.value = list.length
	}
}

const formRef = ref()
// 赋值前先处理时间范围

formModel.value = { ...props.nowRowData, ...props.formData }
const onSubmit = async (form, done) => {
	console.log('dsdsd',form)
	// 处理时间段
	if (form.taskTimeRange) {
		form.startTime = form.taskTimeRange[0]
		form.endTime = form.taskTimeRange[1]
		form.taskTimeRange = undefined
	}

	if (props.formType == 'add') {
		form.organizationIdList = form.directLevel == 1?creditCodeList.value:undefined
		form.userIdList = form.directLevel == 2?personIdList.value:undefined
		apiMod.view_bscExamineTask_add(form).then(res=>{
			emit('refresh')
			done()
		}).catch(()=>{
			emit('refresh')
			done()
		})
	} else {
		form.organizationIdList = form.directLevel == 1?creditCodeList.value:undefined
		form.userIdList = form.directLevel == 2?personIdList.value:undefined
		apiMod.view_bscTaskInfo_edit(form).then((res) => {
			emit('refresh')
			done()
		}).catch(()=>{
			emit('refresh')
			done()
		})
	}
}

const onSave = () => {
	formRef.value!.onSubmit()
}

// 打开刷选器
let listType: any = ref('')
const handleOpenSelect = (type) => {
	// 得到打开类型
	selectedData.value = []
	listType.value = type
	// 拿到勾选的数据
	if (props.formType == 'add') {
		drawer.value = true
	} else {
		if(type == 1){
			selectedData.value = orgObjList.value
			// getApiSelect(props.nowRowData.orgIdList,'view_dpServiceOrgBase_view')
			personIdList.value = []
		}
		if(type == 3){
			selectedData.value = personObjList.value
			// getApiSelect(props.nowRowData.personIdList,'view_dpFoundationPractitioners_view')
			orgIdList.value = []
		}
		drawer.value = true
	}
}
let orgIdList = ref([])
let creditCodeList = ref([])
let personIdList = ref([])
let orgObjList = ref([])
let personObjList = ref([])
// 如果是编辑那么赋值
if(props.nowRowData.formType == 'edit'){
	orgIdList.value = props.nowRowData.orgIdList
	personIdList.value = props.nowRowData.personIdList
}
const onSelectionChange = (arr) => {
	console.log('勾选', arr)
	// 立即查看时回显
	if (listType.value == 1) {
		orgObjList.value = JSON.parse(JSON.stringify(arr))
		OrgNum.value = arr.length
		orgIdList.value = arr.map((el) => {
			return el.id
		})
		creditCodeList.value = arr.map((el) => {
			return el.creditCode
		})
	} else if (listType.value == 3) {
		personObjList.value = JSON.parse(JSON.stringify(arr))
		personNum.value = arr.length
		personIdList.value = arr.map((el) => {
			return el.idCard
		})
	}
	selectedData.value = arr
}


// 选中的表格数据
let selectedData = ref([])
defineExpose({
	onSave,
})
</script>
<style scoped lang="scss"></style>
